package org.charry.lib.commons.io;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * IO utility.
 * 
 * @author wcharry
 * 
 */
public final class IOUtil {
	private static Log log = LogFactory.getLog(IOUtil.class);

	/**
	 * Dummy constructor.
	 */
	private IOUtil() {
		// NOOP
	}

	/**
	 * A function to close InputStream.
	 * 
	 * @param o
	 *            InputStream instance
	 */
	public static void close(final InputStream o) {
		if (o != null) {
			try {
				o.close();
			} catch (IOException e) {
				log.error(e);
			}
		}
	}

	public static void close(final OutputStream o) {
		if (o != null) {
			try {
				o.close();
			} catch (IOException e) {
				log.error(e);
			}
		}
	}

	public static void close(final Reader o) {
		if (o != null) {
			try {
				o.close();
			} catch (IOException e) {
				log.error(e);
			}
		}
	}

	public static void close(final Writer o) {
		if (o != null) {
			try {
				o.close();
			} catch (IOException e) {
				log.error(e);
			}
		}
	}

	/**
	 * A function to delete file.
	 * 
	 * @param f
	 *            file handler
	 * @return true if deleting is successful
	 */
	public static boolean delete(final File f) {
		return (f != null) ? f.delete() : false;
	}
}
